interface Params {
    url: string;
    opts: Object;
    method?: "get" | "post";
    contentType?: "application/x-www-form-urlencoded" | "application/json";
}
export async function getData<T>({ url, opts, method = "get", contentType = "application/json" }: Params): Promise<T> {
    const config = useRuntimeConfig();
    const { data } = await useFetch(url, {
        method,
        // baseURL: config.public.baseURL as string,
        // 请求拦截
        onRequest({ request, options }) {
            console.log("开始");
            // 设置请求头
            options.headers = { "Content-Type": contentType };
            // 设置请求参数
            if (method === "post") {
                options.body = { ...opts };
            } else {
                options.query = { ...opts };
            }
        },
        // 响应拦截
        onResponse({ response }) {
            // 处理响应数据
            console.log(response);
            if (response._data.error) {
                console.warn("=== error url: ", url, "\n params:", opts, "\n response:", response._data);
                // showToast(response._data.message);
            } else {
                return response;
            }
        },
        onRequestError({ request, options, error }) {
            console.log("请求", 123123);
            // 处理请求错误
            // console.warn('request error', error);
            // showToast('Request Error');
        },
        onResponseError({ request, response, options }) {
            console.log("回复", 123123);
            // 处理响应错误
            // console.warn('request error', response);
            // showToast('Request Error');
        },
    });
    return data.value;
}
